﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ITProject.Repository.Abstract;
using ITProject.Repository.Concrete;
using ITProject.Domain.Entities;

namespace SVNClient
{
    /// <summary>
    /// Interaction logic for SelectProjectControl.xaml
    /// </summary>
    public partial class SelectProjectControl : UserControl
    {
        private IProjectRepository _projectRepository;
        private string userLogin;
        private IList<Project> projects;


        public SelectProjectControl(string login)
        {
            InitializeComponent();
            userLogin = login;
            App.Current.MainWindow.SizeChanged += new SizeChangedEventHandler(MainWindow_SizeChanged);
        }

        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Width = App.Current.MainWindow.Width;
            this.Height = App.Current.MainWindow.Height;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = SelectProjectListBox.SelectedIndex;
            if (selectedIndex > -1)
            {
                MainWindow win = (MainWindow)Window.GetWindow(this.Parent);
                win.RootContent.Children.Clear();
                win.RootContent.Children.Add(new SVNMainContent(userLogin, selectedIndex+1));
            }
        }

        private void ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            _projectRepository = new ProjectRepository();
            projects = _projectRepository.GetProjectByOwnerLogin(userLogin);
            if (projects != null && projects.Any())
            {
                foreach (var item in projects)
                {
                    SelectProjectListBox.Items.Add(item.Name);
                }
            }
            else
            {
                SelectProjectListBox.Items.Add("Brak projektów");
            }
        }
    }
}
